// miniprogram/pages/addDetail/addDetail.js
import {listCategory, listType} from '../../apis/type'
import {collectOrder_} from '../../apis/order'
import util from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog: false,
    itemShow: false,
    categoryShow: false,
    id: '',
    details: [],
    weight: '',
    categoryId: '',
    categoryName: '',
    itemId: '',
    itemName: '',
    categoryIds: [],
    categoryNames: [],
    itemIdss: [],
    itemNames: []
  },

  toAddItem() {
    this.setData({
      showDialog: true
    })
  },

  async finish() {
    if (this.data.details === null || this.data.details.length === 0) {
      wx.showToast({
        title: '请添加回收物',
      })
      return ;
    }
    const res = await collectOrder_({
      id: this.data.id,
      details: this.data.details.map(e => {
        return {
          categoryId: e.categoryId,
          itemId: e.itemId,
          weight: util.kgTog(e.weight)
        }
      })
    });
    if (res.code == 200) {
      this.setData({
        details: []
      })
      wx.showToast({
        title: '回收成功',
      })
      wx.redirectTo({
        url: '/pages/orderDetail/orderDetail?id=' + this.data.id,
      })
    } else {
      wx.showToast({
        title: res.message,
      })
    }
  },
  onWeightChange(event) {
    this.setData({
      weight: event.detail,
    });
  },

  onDialogConfirm() {
    console.log(util.countPoint(this.data.weight));
    if (util.countPoint(this.data.weight) > 3) {
      wx.showToast({
        title: '添加失败,重量最多保留3位小数',
        icon: 'none'
      })
      return ;
    }
    let list = this.data.details;
    list.push({
      itemName: this.data.itemName,
      itemId: this.data.itemId,
      categoryId: this.data.categoryId,
      categoryName: this.data.categoryName,
      weight: this.data.weight
    })
    this.setData({
      details: list
    })

    this.setData({
      weight: '',
      categoryId: '',
      categoryName: '',
      itemId: '',
      itemName: '',
      itemIds: [],
      itemNames: []
    })
  },

  onDialogCancel() {
    this.setData({
      weight: '',
      categoryId: '',
      categoryName: '',
      itemId: '',
      itemName: '',
      itemIds: [],
      itemNames: []
    })
  },

  onItemClose() {
    this.setData({
      itemShow: false
    })
  },

  onItemConfirm(e) {
    const d = e.detail;
    this.setData({
      itemShow: false,
      itemId: this.data.itemIds[d.index],
      itemName: d.value
    })
  },

  showItemDialog() {
    if (this.data.categoryId === '') {
      wx.showToast({
        title: '请先选择类别',
      })
      return ;
    }
    this.setData({
      itemShow: true
    })
  },

  onCategoryClose() {
    this.setData({
      categoryShow: false
    })
  },

  showCategoryDialog() {
    this.setData({
      categoryShow: true
    })
  },

  async onCategoryConfirm(e) {
    console.log(e);
    const d = e.detail;
    this.setData({
      categoryShow: false,
      categoryId: this.data.categoryIds[d.index],
      categoryName: d.value
    })

    const res = await listType(this.data.categoryIds[d.index]);
    if (res.code === 200) {
      this.setData({
        itemIds: res.data.map(e => {
          return e.id
        }),
        itemNames: res.data.map(e => {
          return e.name;
        })
      })
    } else {
      console.log(res.message);
    }
  },

  async init() {
    const res = await  listCategory();
    if (res.code === 200) {
      this.setData({
        categoryIds: res.data.map(e => {
          return e.id
        }),
        categoryNames: res.data.map(e => {
          return e.name;
        })
      })
    } else {
      console.log(res.message);
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    console.log(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})